package tcp;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONString;
import cn.hutool.json.JSONUtil;

import java.io.*;
import java.net.Socket;
import java.util.Base64;

public class Testshangbao {

    private static final String ip = "101.132.168.59";
    private static final Integer port = 11081;
    //    private static final String ip = "127.0.0.1";
//    private static final Integer port = 9088;
    private static final String appId = "315168ec596d4d102302488414801470";
    private static final String serviceId = "69b1e2606a4d8bf732ebd8e2a3836b0d";
    private static final String dataId = "90134c28c31b49ea85e17bb90ff32eef";
    private static final String SECRET_KEY = "7520fde709b67dcb";
    private static final String DEVICE_ID = "test_public_works_001";
    private static final String GATEWAY_ID = "public_works_gateway_001";
    private static final String GUOTA_ID = "240712466@001";
    private static final String GUOTA_ID2 = "240712467@001";
    private static final String enterpriseId = "";
    private static final String category = "7";


    /**
     * 仅供参考，具体加密数据以文档规范为准
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String encrypt = encrypt();
        String data = "{\"appId\":\"" + appId + "\",\"serviceId\":\"" + serviceId + "\",\"dataId\":\"" + dataId + "\"," +
                "\"data\":\"" + encrypt + "\"}@@";
        System.out.println(data);

        Socket socket = new Socket(ip, port);
        // 向服务端程序发送数据
        OutputStream ops = socket.getOutputStream();
        OutputStreamWriter opsw = new OutputStreamWriter(ops);
        BufferedWriter bw = new BufferedWriter(opsw);

        bw.write(data);
        bw.flush();

        // 从服务端程序接收数据
        InputStream ips = socket.getInputStream();
        InputStreamReader ipsr = new InputStreamReader(ips);
        BufferedReader br = new BufferedReader(ipsr);
        String s = "";
        while ((s = br.readLine()) != null)
            System.out.println(s);
        socket.close();
    }

    public static String encrypt() throws Exception {

        JSONArray array = JSONUtil.createArray();
        JSONObject obj1 = JSONUtil.createObj();
        obj1.set("quotaId", GUOTA_ID).set("value", 267.63).set("isValid", true);
        array.add(obj1);
        JSONObject obj2 = JSONUtil.createObj();
        obj2.set("quotaId", GUOTA_ID2).set("value", 267.63).set("isValid", true);

        array.add(obj2);

        JSONObject obj = JSONUtil.createObj();
        obj.set("dataId", dataId)
                .set("enterpriseId", enterpriseId)
                .set("gatewayId", GATEWAY_ID)
                .set("collectTime", System.currentTimeMillis())
                .set("isConnectDataSource", true)
                .set("reportType", "report")
                .set("enterpriseId", "0")
                .set("category", category)
                .set("datas", array);
        //要加密的内容
        String plainText = obj.toString();

/*                "{\n" +
                "\t\"dataId\": \"" + dataId + "\",\n" +
                "\t\"enterpriseId\": \"" + enterpriseId + "\",\n" +
                "\t\"gatewayId\": \"" + GATEWAY_ID + "\",\n" +
                "\t\"collectTime\": \"20240723123456\",\n" +
                "\t\"isConnectDataSource\": true,\n" +
                "\t\"reportType\": \"report\",\n" +
                "\t\"enterpriseId\": \"0\",\n" +
                "\t\"category\": " + category + ",\n" +
                "\t\"datas\": [\n" +
                "\t\t{\n" +
                "\t\t\t\"quotaId\": \"" + GUOTA_ID + "\",\n" +
                "\t\t\t\"value\": 12267.63,\n" +
                "\t\t\t\"isValid\": true\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"quotaId\": \"" + GUOTA_ID2 + "\",\n" +
                "\t\t\t\"value\": 12267.63,\n" +
                "\t\t\t\"isValid\": true\n" +
                "\t\t}\n" +
                "\t]\n" +
                "}";*/
//        密钥
        String key = SECRET_KEY;
//        偏移量
        String iv = "0000000000000000";
        System.out.println(plainText);
        System.out.println("------------");
        byte[] encryptedBytes = AESEncryptionExample.encrypt(plainText, key);
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        System.out.println(encryptedText);
        return encryptedText;
//        String decryptedText = decrypt(encryptedBytes, key, iv);
//        System.out.println("Decrypted Text: " + decryptedText);
    }
}
